cmake_minimum_required (VERSION 2.4)
project (owlogmon)
if (COMMAND cmake_policy)
	cmake_policy (SET CMP0003 NEW)
	cmake_policy (SET CMP0005 NEW)
endif (COMMAND cmake_policy)

include (CheckFunctionExists)
include (CheckSymbolExists)

check_symbol_exists (HW_SENSORS "sys/param.h;sys/sysctl.h" HAS_BSD_SENSORS)
check_symbol_exists (FNM_CASEFOLD "fnmatch.h" HAS_FNM_CASEFOLD)
check_symbol_exists (RANDOM_ERANDOM "sys/sysctl.h" HAVE_RANDOM_ERANDOM)
check_function_exists (sysinfo HAS_SYSINFO)
check_function_exists (arc4random HAS_ARC4RANDOM)
check_function_exists (arc4random_uniform HAS_ARC4RANDOM_UNIFORM)

# Just be safe and save default list.
set (OLD_CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})

# BSD interface used to list processes
set (CMAKE_REQUIRED_LIBRARIES kvm)
check_function_exists (kvm_openfiles HAS_KVM_OPENFILES)
check_function_exists (kvm_getproc2 HAS_KVM_GETPROC2)
set (CMAKE_REQUIRED_LIBRARIES ${OLD_CMAKE_REQUIRED_LIBRARIES})

# imsg framework, since OpenBSD 4.8
set (CMAKE_REQUIRED_LIBRARIES ${OLD_CMAKE_REQUIRED_LIBRARIES} util)
check_function_exists (imsg_init HAS_IMSG)

# Play nice, reset list after use
set (CMAKE_REQUIRED_LIBRARIES ${OLD_CMAKE_REQUIRED_LIBRARIES})

if (NOT HAS_FNM_CASEFOLD)
	set (FNM_CASEFOLD_OBJECTS "${PROJECT_SOURCE_DIR}/strcase.c")
endif (NOT HAS_FNM_CASEFOLD)

if (HAS_BSD_SENSORS)
	set (BSD_SENSORS_OBJECTS "${PROJECT_SOURCE_DIR}/sensor_bsd.c")
endif (HAS_BSD_SENSORS)

if (NOT HAS_ARC4RANDOM OR NOT HAS_ARC4RANDOM_UNIFORM)
	set (PRNG_DEV "/dev/urandom" CACHE FILEPATH "Random generator")
	set (ADD_OBJECTS ${ADD_OBJECTS}
	    "${PROJECT_SOURCE_DIR}/../compat/arc4random.c")
endif (NOT HAS_ARC4RANDOM OR NOT HAS_ARC4RANDOM_UNIFORM)

if (HAS_KVM_OPENFILES)
	set (KVM_LIBS kvm)
endif (HAS_KVM_OPENFILES)

add_definitions (-Wall)
set (COMMON_OBJECTS bitmap.c peertask.c peer.c timestamp.c resolver.c)
set (ASSL_OBJECTS "../assl/assl.c")
# set (IMSG_OBJECTS "../compat/imsg-buffer.c" "../compat/imsg.c")
add_executable (owlogmon  owlogmon.c ${COMMON_OBJECTS} ${ADD_OBJECTS}
	${ASSL_OBJECTS} ${IMSG_OBJECTS} logmsg.c sensor.c syslogd.c watchdog.c
	sensor_loadavg.c sensor_log_stream.c sensor_syslog_bsd.c
	${FNM_CASEFOLD_OBJECTS} ${BSD_SENSORS_OBJECTS})
add_executable (owlogcoll owlogcoll.c ${COMMON_OBJECTS} ${ADD_OBJECTS}
	${ASSL_OBJECTS})
add_executable (owfilter  owfilter.c ${COMMON_OBJECTS} ${ADD_OBJECTS})
target_link_libraries (owlogmon crypto ssl ${KVM_LIBS})
target_link_libraries (owlogcoll crypto ssl)
set_target_properties (owfilter PROPERTIES COMPILE_FLAGS -DNO_SSL=1)
install (TARGETS owlogmon owlogcoll owfilter DESTINATION bin)
